package com.quarantine.util;

import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;

import com.sun.org.apache.xerces.internal.impl.xpath.regex.ParseException;

/**
 * 时间基本操作
 * 
 * @author hqm
 * @email hqm1988.good@163.com
 * @createTime 2012-4-18 下午3:11:41
 * 
 */
public class DateUtils {
	private static final DateFormat dformat_full = new SimpleDateFormat(
			"yyyy-MM-dd HH:mm:ss");
	private static final DateFormat dformat_day = new SimpleDateFormat(
			"yyyy-MM-dd");
	private static final DateFormat dformat_day_2 = new SimpleDateFormat(
			"yyyy年MM月dd日");

	private DateUtils() {
	}

	/**
	 * 获取现在的时间，精确到秒
	 * 
	 * @return
	 */
	public static String now() {
		return dformat_full.format(new Date());
	}

	/**
	 * 获取今天的日期 yyyy-MM-dd
	 * 
	 * @return
	 */
	public static String today() {
		return dformat_day.format(new Date());
	}

	/**
	 * 获取yyyy年MM月dd日
	 * 
	 * @return
	 */
	public static String today2() {
		return dformat_day_2.format(new Date());
	}

	/**
	 * 查找前N天的日期
	 * 
	 * @param dateString
	 * @param beforeDays
	 * @return
	 * @throws ParseException
	 * @throws java.text.ParseException
	 */
	public Date getDate(String dateString, int beforeDays)
			throws ParseException, java.text.ParseException {
		//DateFormat dateFormat = new SimpleDateFormat("yyyy-dd-MM");
		Date inputDate = dformat_day.parse(dateString);
		Calendar cal = Calendar.getInstance();
		cal.setTime(inputDate);
		int inputDayOfYear = cal.get(Calendar.DAY_OF_YEAR);
		cal.set(Calendar.DAY_OF_YEAR, inputDayOfYear - beforeDays);
		return cal.getTime();
	}
}
